# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from boss.items import BossItem


class ZhipinSpider(CrawlSpider):
    name = 'zhipin'
    allowed_domains = ['zhipin.com']
    start_urls = ['http://zhipin.com/']
    'https://www.zhipin.com/job_detail/482b8c13fe2563cd1HF93dW-GVE~.html'
    'https://www.zhipin.com/job_detail/89a8b9eec84546ea0n1-2Nu8ElU~.html'
    'https://www.zhipin.com/job_detail/e0d06a6632a247be0nB92dS9EVE~.html'
    'https://www.zhipin.com/job_detail/071775f7619a85170XR92dq5Els~.html?ka=search_list_3'
    'https://www.zhipin.com/job_detail/e226906bef4dfae90Xx82t60EVU~.html?ka=search_list_91'

    rules = (
        # 匹配列表页的规则，一共有几页，每一页的链接
        Rule(LinkExtractor(allow=r'.+\?query=Python&page=\d+'), follow=True),
        # 匹配详情页的规则
        Rule(LinkExtractor(allow=r'https://www.zhipin.com/job_detail/.+~\.html'), callback='parse_job', follow=False),
    )

    # 获取招聘信息
    def parse_job(self, response):
        # TODO 这里的数据解析需要测试一下
        # 获取最上面的主要工作信息
        job_primary = response.xpath('//div[contains(@class,"job-primary") and contains(@class,"detail-box")]')
        # 工作状态
        job_status = job_primary.xpath('.//div[@class="job-status"]/text()').get()
        # 职位名称
        job_name = job_primary.xpath('.//div[@class="name"]/h1/text()').get()
        # 薪资
        salary = job_primary.xpath('.//div[@class="name"]/span[@class="salary"]/text()').extract_first()
        # 地址、年份、学历
        others = job_primary.xpath('.//div[@class="info-primary"]/p//text()').getall()
        address = others[0]
        work_years = others[1]
        education = others[2]
        # 工作福利
        job_welfare = []
        job_tags = job_primary.xpath('.//div[@class="tag-container"]/div[@class="job-tags"]//text()').getall()
        for welfare in job_tags:
            job_welfare.append(welfare)

        # 工作详细描述
        job_detail = response.xpath('//div[@class="job-detail"]')
        # 工作发布者头像、工作发布者的名字、工作发布者的职位
        publisher_figure = job_detail.xpath(
            './/div[@class="detail-op"]/div[@class="detail-figure"]/img/@src').extract_first()
        publisher_name = job_detail.xpath('.//div[@class="detail-op"]/h2[@class="name"]//text()').get()
        publisher_position = job_detail.xpath('.//p[@class="gray"]//text()').getall()[0]
        # 职位描述
        job_sec = '\n'.join(
            job_detail.xpath('.//div[@class="detail-content"]/div[1]/div[@class="text"]//text()').getall())
        # 公司介绍
        company_info = '\n'.join(job_detail.xpath('.//div[@class="company-info"]/div[@class="text"]//text()').getall())
        # 工商信息
        # 公司全称、法人代表、注册资金、成立时间、企业类型、经营状态
        company_fullname = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="name"]/text()').extract_first()
        representative = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="level-list"]/li[1]/text()').get()
        registered_capital = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="level-list"]/li[2]/text()').get()
        establishment_time = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="level-list"]/li[3]/text()').get()
        company_type = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="level-list"]/li[4]/text()').get()
        management_state = job_detail.xpath(
            './/div[@class="detail-content"]/div[4]/div[@class="level-list"]/li[5]/text()').get()
        '/html/body/div[1]/div[2]/div[3]/div/div[2]/div[2]/div[5]'
        '/html/body/div[1]/div[2]/div[3]/div/div[2]/div[2]/div[4]'
        # 工作详细地址名称、工作地址图片
        location_address = job_detail.xpath(
            './/div[@class="detail-content"]/div[5]//div[@class="location-address"]/text()').get()
        location_map = job_detail.xpath('.//div[@class="detail-content"]/div[5]//img/@src').get()

        # 公司基本信息
        # 公司的简称，公司的图标、融资、公司人数、公司定位、公司网址、更新时间
        company_base_info = response.xpath('.//div[@class="sider-company"]')
        company_abbreviation = company_base_info.xpath('./div[@class="company-info"]/a[2]/text()').get().split()
        company_icon = company_base_info.xpath('./div[@class="company-info"]//img/@src').get()
        financing = company_base_info.xpath('./div[@class="company-info"]/p[2]/text()').get()
        company_number = company_base_info.xpath('./div[@class="company-info"]/p[3]/text()').get()[:-1]
        direction = company_base_info.xpath('./div[@class="company-info"]/p[4]/a/text()').get()
        net = company_base_info.xpath('./div[@class="company-info"]/p[5]/text()').get()
        update_time = company_base_info.xpath('./div[@class="company-info"]/p[6]/text()').get()[4:]

        item = BossItem(job_status=job_status, job_name=job_name, salary=salary, address=address, work_years=work_years,
                        education=education, job_welfare=job_welfare, publisher_figure=publisher_figure,
                        publisher_name=publisher_name,
                        publisher_position=publisher_position, job_sec=job_sec, company_info=company_info,
                        company_fullname=company_fullname,
                        representative=representative, registered_capital=registered_capital,
                        establishment_time=establishment_time,
                        company_type=company_type, management_state=management_state, location_address=location_address,
                        location_map=location_map, company_abbreviation=company_abbreviation, company_icon=company_icon,
                        financing=financing, company_number=company_number, direction=direction, net=net,
                        update_time=update_time)

        yield item
